﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using HP.Cachalote.DataModels;

namespace HP.Cachalote.Repositories.NHibernate.DataMappings
{
    public class TodoLabelMap : ClassMap<TodoLabel>
    {
        public TodoLabelMap()
        {
            Id(o => o.ID).GeneratedBy.Identity();
            Map(o => o.Name);
            Map(o => o.Icon);
            Map(o => o.OrderIndex);
            References<TodoLabel>(o => o.Parent).Column("ParentID");

            //Inverse on HasMany is an NHibernate term, and it means that the other end 
            //of the relationship is responsible for saving.
            //Cascade.All tells NHibernate to cascade events down to the entities in the 
            //collection (so when you save the Label, all the TodoItems are saved too).
            HasMany<TodoItem>(o => o.TodoItems).KeyColumn("LabelID").Cascade.All();

            HasMany<TodoLabel>(o => o.SubLabels).KeyColumn("ParentID").Cascade.All();
            
            Table("Labels");
        }
    }
}
